﻿Imports System.Globalization
Imports Engage.Dnn.Publish
Imports Engage.Dnn.Publish.Util

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers

    Public Class EngagePublishUrlRewriter
        Implements IUrlRewriterProvider, ICategoryUrlRewriterProvider, IIsActive



        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites
            Dim toReturn As New List(Of GroupParamsRewrite)
            Dim desc As String = "Title of the current Engage Publish article"
            toReturn.Add(New GroupParamsRewrite("ArticleTitle", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            desc = "List of the current engage publish article tags, separated by the default char replacement for a space char"
            toReturn.Add(New GroupParamsRewrite("TagNames", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            desc = "Parent category of the current engage publish article"
            toReturn.Add(New GroupParamsRewrite("ParentCategoryName", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            desc = "first related category of the current engage publish article"
            toReturn.Add(New GroupParamsRewrite("RelatedCategoryName1", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            desc = "second related category of the current engage publish article"
            toReturn.Add(New GroupParamsRewrite("RelatedCategoryName2", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            desc = "third related category of the current engage publish article"
            toReturn.Add(New GroupParamsRewrite("RelatedCategoryName3", desc, RewriteType.SubPath, UrlParam.FromSyntax("itemid")))
            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                       ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult _
            Implements IUrlRewriterProvider.RewriteParams
            Dim toReturn As New GroupRewriteResult()

            If params.Count > 0 Then
                Dim itemId As Integer = Integer.Parse(params(UrlParam.FromSyntax("itemid")), CultureInfo.InvariantCulture)
                Dim objItemType As Util.ItemType = ItemType.GetFromId(Item.GetItemTypeId(itemId, NukeHelper.PortalId), GetType(ItemType))
                Dim isArticle As Boolean = (objItemType.GetId = ItemType.Article.GetId)
                Select Case groupName.ToLowerInvariant
                    Case "articletitle"
                        If isArticle Then
                            toReturn.RewriteValue = GetArticleTitle(itemId, behaviour)
                            toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = True
                        End If
                    Case "tagnames"
                        If isArticle Then
                            For Each strTag As String In Me.GetTagNames(itemId, behaviour)
                                toReturn.AddRewriteValue(strTag)
                                toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = False
                            Next
                        End If
                    Case "parentcategoryname"
                        If isArticle Then
                            toReturn.RewriteValue = GetParentCategoryNameByArticle(itemId, behaviour)
                        Else
                            toReturn.RewriteValue = GetCategoryName(itemId, behaviour)
                        End If
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = False
                    Case "relatedcategoryname1"
                        If isArticle Then
                            toReturn.RewriteValue = GetRelatedCategoryNameByArticleId(itemId, 0, behaviour)
                        Else
                            toReturn.RewriteValue = GetCategoryName(itemId, behaviour)
                        End If
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = False
                    Case "relatedcategoryname2"

                        If isArticle Then
                            toReturn.RewriteValue = GetRelatedCategoryNameByArticleId(itemId, 1, behaviour)
                            toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = False
                        End If
                    Case "relatedcategoryname3"

                        If isArticle Then
                            toReturn.RewriteValue = GetRelatedCategoryNameByArticleId(itemId, 2, behaviour)
                            toReturn.ConsumedParameters(UrlParam.FromSyntax("itemid")) = False
                        End If

                End Select
            End If
            Return toReturn
        End Function


        Public Function GetArticleTitle(ByVal itemId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String

            Dim objArticle As Article = Article.GetArticle(itemId, NukeHelper.PortalId)
            Dim toReturn As String = objArticle.Name
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetThematiqueIdByArticle(ByVal itemId As Integer) As Integer
            Dim toReturn As Integer = -1
            Dim objArticle As Article = Article.GetArticle(itemId, NukeHelper.PortalId)
            If objArticle IsNot Nothing Then
                Dim list As ArrayList = ItemRelationship.GetItemRelationships(itemId, objArticle.ItemVersionId, Util.RelationshipType.ItemToRelatedCategory.GetId, True, NukeHelper.PortalId)
                If list.Count > 0 Then
                    Dim objItemRelationship As ItemRelationship = list(0)
                    toReturn = objItemRelationship.ParentItemId
                End If
            End If
            Return toReturn

        End Function

        Public Function GetParentCategoryNameByArticle(ByVal itemId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim toReturn As String = ""
            Dim objArticle As Article = Article.GetArticle(itemId, NukeHelper.PortalId)
            If objArticle IsNot Nothing Then
                Dim list As ArrayList = ItemRelationship.GetItemRelationships(itemId, objArticle.ItemVersionId, Util.RelationshipType.ItemToParentCategory.GetId, True, NukeHelper.PortalId)
                If list.Count > 0 Then
                    Dim objItemRelationship As ItemRelationship = list(0)
                    Dim objCategory As Category = Category.GetCategory(objItemRelationship.ParentItemId, NukeHelper.PortalId)
                    toReturn = objCategory.Name
                End If
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetCategoryName(ByVal categoryId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim toReturn As String = ""
            Dim objCategory As Category = Category.GetCategory(categoryId, NukeHelper.PortalId)
            If objCategory IsNot Nothing Then
                toReturn = objCategory.Name
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetRelatedCategoryNameByArticleId(ByVal itemId As Integer, ByVal categoryIndex As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim toReturn As String = ""
            Dim objArticle As Article = Article.GetArticle(itemId, NukeHelper.PortalId)
            If objArticle IsNot Nothing Then
                Dim list As ArrayList = ItemRelationship.GetItemRelationships(itemId, objArticle.ItemVersionId, Util.RelationshipType.ItemToRelatedCategory.GetId, True, NukeHelper.PortalId)
                If list.Count > categoryIndex - 1 Then
                    Dim objItemRelationship As ItemRelationship = list(categoryIndex)
                    Dim objCategory As Category = Category.GetCategory(objItemRelationship.ParentItemId, NukeHelper.PortalId)
                    toReturn = objCategory.Name
                End If
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetTagNames(ByVal itemId As Integer, ByVal behaviour As FriendlierUrlStrategy) As List(Of String)
            Dim toReturn As New List(Of String)
            Dim objArticle As Article = Article.GetArticle(itemId, NukeHelper.PortalId)
            If ((Not objArticle Is Nothing) AndAlso objArticle.Tags.Count < 1) Then
                Dim itemTags As ArrayList = ItemTag.GetItemTags(objArticle.ItemVersionId, NukeHelper.PortalId)
                If itemTags.Count > 0 Then
                    For Each objItemTag As ItemTag In itemTags
                        toReturn.Add(Tag.GetTag(objItemTag.TagId, NukeHelper.PortalId).Name)
                    Next
                    'toReturn = toReturn.Remove(toReturn.Length - behaviour.StringEscaper.GetDefaultCharReplacement().Length)
                End If
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetCategory(ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String)) As Integer Implements ICategoryUrlRewriterProvider.GetCategory

            Dim categoryId As Integer = -1

            If params.ContainsKey(UrlParam.FromSyntax("itemid")) Then
                Dim itemId As Integer = Integer.Parse(params(UrlParam.FromSyntax("itemid")), CultureInfo.InvariantCulture)
                Dim objItemType As Util.ItemType = ItemType.GetFromId(Item.GetItemTypeId(itemId, NukeHelper.PortalId), GetType(ItemType))
                Dim isArticle As Boolean = (objItemType.GetId = ItemType.Article.GetId)

                If isArticle Then
                    categoryId = GetThematiqueIdByArticle(itemId)
                Else
                    categoryId = itemId
                End If
            End If

            Return categoryId

        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return DotNetNuke.Framework.Reflection.CreateType("Engage.DNN.Publish.Article", True) IsNot Nothing
            End Get
        End Property
    End Class
End Namespace


